package org.jupiter.blog.common.utils;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class RSAKeyGeneratorUtil {

	private static final KeyPair keyPair;

	static {
		try{
			// 初始化RSA密钥对
			KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
			keyPairGenerator.initialize(2048);
			keyPair = keyPairGenerator.generateKeyPair();
		} catch (NoSuchAlgorithmException e){
			throw new RuntimeException("Failed to generate RSA key pair", e);
		}
	}

	// 获取公钥（用于前端）
	public static String getPublicKey() {
		PublicKey publicKey = keyPair.getPublic();
		return Base64.getEncoder().encodeToString(publicKey.getEncoded());
	}

	// 获取私钥（后端内部使用）
	public static PrivateKey getPrivateKey() {
		return keyPair.getPrivate();
	}
}
